import JobAdminBizClient
from model import jobHandlerDao
from model.jobHandlerDao import jobThreadDao
from thread.logFileThread import logFileAppender, logFileClean
from thread.triggerCallBackThread import triggerCallBackThread


# 启动JobExecutorServer 作为对任务CURD的apiServer

class EmbedServer:
    pass


class JobExecutorServer:

    def __init__(self,
                 logger,
                 adminAdress,
                 accessToken,
                 appname,
                 ip,
                 port,
                 logPath,
                 logRetentionDays,
                 adminBizList,
                 embedServer,
                 jobHandlerRepository):
        self.adminAdress = None
        self.accessToken = None
        self.appname = None
        self.ip = None
        self.port = None
        self.adminBizList = None
        self.embedServer = None
        self.jobHandlerRepository = None

    # 参数set
    def setParams(self):
        return str

    # 任务执行相关服务启动entry
    def start(self):

        logFileAppender.initLogpath(self.logPath)
        self.initAdminBizList(self.adminBizList, self.accessToken)
        logFileClean.getInstance().start(self.logRetentionDays)

        triggerCallBackThread.getInstance().start()
        self.initEmbedServer(self.address, self.accessToken)

    # 初始化调度器 服务器 list
    def initAdminBizList(self, adminAddress, accessToken):
        adminBiz = JobAdminBizClient(adminAddress, accessToken)
        adminBizlist = []
        adminBizlist.append(adminBiz)

    # 启动执行器内嵌Server， 默认端口9999
    def initEmbedServer(self, address, accessToken):
        embedServer = EmbedServer()
        embedServer.start(address, accessToken)

    def stopEmbedServer(self):
        self.embedServer.stop()

    # 加载具体任务(get)
    def loadJobHandler(self, name):
        return jobHandlerDao.get(name)

    def loadJobThread(self, id):
        return jobThreadDao.get(id)

    # 注册任务(put)
    def registJobHandler(self, name):
        return jobHandlerDao.put(name)

    def registJobThead(self, id):
        return jobThreadDao.put(id)



